package com.mall.manager.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.mall.manager.service.ItemCatService;
import com.mall.manager.dao.TbItemCatMapper;
import com.mall.manager.pojo.TbItemCat;
import com.mall.manager.pojo.TbItemCatExample;
import com.mall.pojo.EasyUiTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
@Component
@Transactional
public class ItemCatServiceImp implements ItemCatService {

    @Autowired
    private TbItemCatMapper itemCatMapper;


    @Override
    public List<EasyUiTreeNode> listItemCat(Long parentId) {

        TbItemCatExample itemCatExample = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = itemCatExample.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> tbItemCats = itemCatMapper.selectByExample(itemCatExample);

        List<EasyUiTreeNode> list = new ArrayList<>();
        //创建返回结果的list
        for (TbItemCat tbItemCat : tbItemCats) {
            EasyUiTreeNode treeNode = new EasyUiTreeNode();
            treeNode.setId(tbItemCat.getId());
            treeNode.setText(tbItemCat.getName());
            treeNode.setState(tbItemCat.getIsParent()?"closed":"open");
            list.add(treeNode);
        }
        return list;
    }
}
